import { apiBaseUrl } from './config.js';
import * as common from './commons.js' //引入common
import * as db from './db.js' //引入common
const methodsToken = [

]
// H5登录code
export const wxLogin = (data, callback) => post('login', data, callback, '', true);
// 免登录接口
// export const wxLogin = (data, callback) => get('wxLogin', data, callback, '', true);
// 仪器申请审批 deviceApplyExamine
export const deviceApplyExamine = (data, callback) => post('deviceApplyExamine', data, callback, '', true);
// 仪器借用申请 deviceBorrowApply
export const deviceBorrowApply = (data, callback) => post('deviceBorrowApply', data, callback, '', true);
// 仪器归还申请 deviceRestitutionApply
export const deviceRestitutionApply = (data, callback) => post('deviceRestitutionApply', data, callback, '', true);
// 获取审批管理员列表 getAdminList
export const getAdminList = (data, callback) => post('getAdminList', data, callback, '', true);
// 获取审批管理员列表 getBorrowList
export const getBorrowList = (data, callback) => post('getBorrowList', data, callback, '', true);
// 获取仪器列表(借用) getDeviceListBorrow
export const getDeviceListBorrow = (data, callback) => post('getDeviceListBorrow', data, callback, '', true);
// 获取仪器列表(归还) getDeviceListRestitution
export const getDeviceListRestitution = (data, callback) => post('getDeviceListRestitution', data, callback, '', true);
// 获取仪器类型列表 getDeviceTypeList
export const getDeviceTypeList = (data, callback) => post('getDeviceTypeList', data, callback, '', true);
// 获取审批列表 getExamineList
export const getExamineList = (data, callback) => post('getExamineList', data, callback, '', true);
// 获取归还申请列表 getRestitutionList
export const getRestitutionList = (data, callback) => post('getRestitutionList', data, callback, '', true);
// 实际归还影像上传 realityFileUpload
export const realityFileUpload = (data, callback) => post('realityFileUpload', data, callback, '', true);


const post = (method, data, callback, complete, isFalseReturn = false) => {
	uni.request({
		url: apiBaseUrl + method,
		method: 'POST',
		data,
		header:{},
		// responseType: 'text',
		// header：: {
		// 	'content-type': 'application/json',
		// 	'Access-Control-Allow-Origin': '*',
		// 	'custom-header': 'application/x-www-form-urlencoded'
		// },
		success: res => {
			if (parseInt(res.statusCode) !== 200) {
				showError({ msg: '系统错误' })
				return false
			}
			var dataCode = res.data
			if (dataCode.code != 200) {
				showError({ msg: dataCode.msg ? dataCode.msg : '请求失败' })
				if (isFalseReturn) {
					callback(false)
				} else {
					callback(false)
				}
			} else {
				callback(res.data.data)
			}

		},
		fail: (error) => {
			console.log(error)
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: (res) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	})
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl, //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				headers: {
					'Accept': 'application/json',
					'Content-Type': 'multipart/form-data',
				},
				formData: {
					'method': 'images.upload',
					'upfile': tempFilePaths[0]
				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 上传图片
export const uploadImage = (num, callback) => {
	uni.chooseImage({
		count: num,
		success: (res) => {
			uni.showLoading({
				title: '上传中...'
			});
			let tempFilePaths = res.tempFilePaths
			for (var i = 0; i < tempFilePaths.length; i++) {
				uni.uploadFile({
					url: apiBaseUrl + 'upload/image',
					filePath: tempFilePaths[i],
					fileType: 'image',
					name: 'file',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
					},
					formData: {
						'method': 'images.upload',
						'upfile': tempFilePaths[i]
					},
					success: (uploadFileRes) => {
						if (JSON.parse(uploadFileRes.data).code == 200) {
							callback(JSON.parse(uploadFileRes.data).data.links);
						}
					},
					fail: (error) => {
						if (error && error.response) {
							showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 250);
					},
				});
			}
		}
	});
}

